-- -------------------------------------------------------------------------- --
-- GridStatusMTs by kunda                                                     --
-- -------------------------------------------------------------------------- --
local GridRoster = GridRoster
if not GridRoster then
	DEFAULT_CHAT_FRAME:AddMessage("GridStatusMTs requires GridRoster.")
	DEFAULT_CHAT_FRAME:AddMessage("Please update Grid: http://www.wowace.com/projects/grid/")
	return
end
local L = GetLocale()

local LOC_MTs = "MTs"
if L == "deDE" then
	LOC_MTs = "MTs"
elseif L == "koKR" then
	LOC_MTs = "메인 탱커"
elseif L == "ruRU" then
	LOC_MTs = "ГТ"
elseif L == "zhCN" then
	LOC_MTs = "MT"
elseif L == "zhTW" then
	LOC_MTs = "主坦克"
end

GridStatusMTs = GridStatus:NewModule("GridStatusMTs")
GridStatusMTs.menuName = LOC_MTs

GridStatusMTs.defaultDB = {
	debug = false,
	alert_mt = {
		text = LOC_MTs,
		enable = true,
		color = { r = 1, g = 1, b = 1, a = 0.4 },
		priority = 60,
		range = false,
	}
}

GridStatusMTs.options = false

function GridStatusMTs:OnInitialize()
	self.super.OnInitialize(self)
	self:RegisterStatus("alert_mt", LOC_MTs, nil, true)
end

function GridStatusMTs:OnStatusEnable(status)
	if status == "alert_mt" then
		self:RegisterEvent("oRA_MainTankUpdate", "UpdateMainTankTable")
		self:RegisterEvent("RAID_ROSTER_UPDATE", "UpdateMainTankTable")
		if CT_RAOptions_UpdateMTs then
			hooksecurefunc("CT_RAOptions_UpdateMTs", GridStatusMTs.UpdateMainTankTable)
		end
		self:UpdateMainTankTable()
	end
end

function GridStatusMTs:OnStatusDisable(status)
	if status == "alert_mt" then
		self:UnregisterEvent("oRA_MainTankUpdate", "UpdateMainTankTable")
		self:UnregisterEvent("RAID_ROSTER_UPDATE", "UpdateMainTankTable")
		self.core:SendStatusLostAllUnits("alert_mt")
	end
end

function GridStatusMTs:UpdateMainTankTable()
	self.core:SendStatusLostAllUnits("alert_mt")
	local settings = self.db.profile.alert_mt
	if not settings.enable then return end -- needed for CT_RA hooksecurefunc
	local maintanktable
	local isBlizzMTs -- nil/false = ADDON | true = BLIZZARD
	if oRA then
		maintanktable = oRA.maintanktable
	end
	if CT_RA_MainTanks then
		maintanktable = CT_RA_MainTanks
	end
	if not maintanktable then
		maintanktable = {}
		local x = 1
		local raid = GetNumRaidMembers()
		if raid > 0 then
			for i = 1, raid do
				local name, _, _, _, _, _, _, _, _, role = GetRaidRosterInfo(i)
				if name and role and role == "MAINTANK" then
					maintanktable[x] = name
					x = x + 1
					if x > 10 then
						break
					end
				end
			end
		end
		if maintanktable[1] then
			isBlizzMTs = true -- nil/false = ADDON | true = BLIZZARD
		else
			maintanktable = nil
		end
	end	

	if not maintanktable then return end
	
	local MTicon = "Interface\\AddOns\\GridStatusMTs\\icons\\mt.tga"
	local MTtext = "MT"

	for i = 1, 10 do
		if maintanktable[i] then
			if not isBlizzMTs then
				MTicon = "Interface\\AddOns\\GridStatusMTs\\icons\\mt"..i..".tga"
				MTtext = "MT"..i
			end

			for guid, unitid in GridRoster:IterateRoster() do
				local name = GridRoster:GetNameByGUID(guid)
				if maintanktable[i] == name then
					self.core:SendStatusGained(guid, "alert_mt",
						settings.priority,
						(settings.range and 40),
						settings.color,
						MTtext,
						nil,
						nil,
						MTicon)
				end
			end

		end
	end
end